import router from './index'
import store from '@/store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {message} from 'ant-design-vue'
import { inject ,ref} from 'vue'


NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/home','/details','/404','/500' , '/space'];
router.beforeEach(async (to,from,next) => {
  NProgress.start();

  let bgImgShow = inject('bgImgShow');
  if(to.path.startsWith('/home')){

    bgImgShow.value = true;
  }else{
    bgImgShow.value = false;
  }
  //其他页面控制
  if(store.getters['user/token'] != ''){

    try{
      const res = await store.dispatch('user/verifyUser');
    }catch(e){
      console.log(e);
    }

    next();
  }else{

    const tempArr = whiteList.filter(item => to.path.startsWith(item));

    if(tempArr.length > 0){
      next();
    }else{
      next({path: '/home'});
      message.warning({
        content: '非法访问',
        duration: 2
      })
      NProgress.done();
    }
  }
})

router.afterEach((to,from,next) => {

  if(to.meta.title){
    document.title = to.meta.title;
  }

  if(to.query.title){
    document.title = to.query.title;
  }
  NProgress.done();
});